#!/bin/bash

function installlang {
	filepath="/usr/share/tesseract-ocr/4.00/tessdata/chi_sim.traineddata"
	tesspath="/usr/share/tesseract-ocr/4.00/tessdata"
	echo "检测中文语言包..."
	if [ -d "$tesspath" ]; then
		echo "已检测到目录"
		if [ -f "$filepath" ]; then
			echo "中文语言包文件已安装"
			# echo "0.5s后自动退出"
			# sleep 0.5
			exit 0
		else
			echo "复制新文件..."
			sleep 0.5
			echo "进入doc目录"
			cd doc
			sudo cp chi_sim.traineddata $tesspath
			if [ -f "$filepath" ]; then
				echo "安装成功"
				echo "2s后自动退出"
				sleep 2
				exit 0
			else
				echo "复制文件失败，请检查其他原因"
				echo "按任意键继续"
				read -n 1
				exit 0
			fi		
		fi
	else
		echo "$tesspath 目录不存在，请检查其他原因，退出..."
		echo "按任意键继续"
		read -n 1
		exit 0
		
	fi
}
cd ..
case $1 in
  
htmltopdf)
	echo "尝试自动安装html转pdf模块..."
	echo "开始执行命令"
	echo "检测python3是否存在"
	if [[ ! -f "./YztyPy/bin/python3" ]]; then
		echo "未检测到python3"
		echo "退出安装"
		echo "按任意键继续"
		read -n 1
		exit 0
	else
		echo "已检测到python3"
	fi
	echo "检测安装状态..."
	string=$(YztyPy/bin/python3 -m pip list)
	if [[ ! $string == *"pdfkit"* ]]; then
		echo "检测到未安装 pdfkit"
		echo "准备开始安装 pdfkit"
		sleep 0.5
		YztyPy/bin/python3 -m pip install pdfkit
	else
		echo "检测到已安装 pdfkit"
	fi
	if [[ ! $string == *"wkhtmltopdf"* ]]; then
		echo "检测到未安装 wkhtmltopdf"
		sleep 0.5
		echo "准备开始安装 wkhtmltopdf"
		YztyPy/bin/python3 -m pip install wkhtmltopdf
	else
		echo "检测到已安装 wkhtmltopdf"
	fi
	echo "准备复制 htmltopdf.py..."
	echo "进入doc目录"
	cd doc
	if [ -f "../YztyPy/htmltopdf.py" ]; then
		echo "文件已存在"
		echo "安装完成"
		# echo "0.5s后自动退出"
		# sleep 0.5
		exit 0
	else
	pwd
		sudo cp htmltopdf.py ../YztyPy
		if [ -f "../YztyPy/htmltopdf.py" ]; then
			echo "复制成功"
			echo "安装成功"
			echo "2s后自动退出"
			sleep 2
			exit 0
		else
			echo "复制失败，请检查其他原因"
			echo "安装失败"
			echo "按任意键继续"
			read -n 1
			exit 0
		fi
	fi
        ;;
installocr)
    echo "检测ocr库..."
	echo "检测是否安装tesseract-ocr..."
	string=$(dpkg -l|grep tesseract)
	if [[ $string == *"Tesseract OCR library"* ]] && [[ $string == *"Tesseract command line OCR tool"* ]]
	then
		echo "已安装"
		installlang
	else
		echo "未安装"
		echo "准备尝试执行自动安装..."
		sleep 0.5
		sudo apt install tesseract-ocr
		installlang
	fi

        ;;
esac
echo "按任意键继续"
read -n 1
exit